package com.xltq.dao.impl;

import com.xltq.dao.IOrdersDao;
import com.xltq.entity.Address;
import com.xltq.entity.Item;
import com.xltq.entity.Orders;
import com.xltq.entity.Product;
import com.xltq.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

//订单数据库表操作
public class IOrdersDaoImpl implements IOrdersDao {
    //提供BDUtils工具类的对象用于操作数据
    QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
    @Override
    public void insertOrders(Orders orders) throws Exception {
        String sql = "insert into orders(o_id,u_id, a_id, o_count, o_time, o_state) values(?,?,?,?,?,?)";
        queryRunner.update(sql,orders.getOid(),orders.getUid(),orders.getAid(),
                orders.getOcount(),orders.getOtime(),orders.getOstate());
    }

    @Override
    public void insertItems(List<Item> items) throws Exception {
            //一个订单中有多个多个订单项,需要使用到DBUtils中 批处理(一次性添加多个数据到数据库中)
            //创建这个参数的二维数据的赋值是
        /**
         * 第一个[] 存的是一共有多个数据数据
         * 第二个[] 存储的每行数据是什么
         */
          //提供一个二维数组定义,这个数组里面是存储这个每一个订单项信息
          Object[][] params = new Object[items.size()][];
          String sql = "insert into item(o_id, p_id, i_count, i_num) values(?,?,?,?)";
          for (int i =0 ;i<items.size();i++){
              Item item = items.get(i);
              params[i] = new Object[]{item.getOid(),item.getPid(),item.getIcount(),item.getInum()};
          }
          //上面代码执行完毕之后,二维数组中就存在存储数据
           queryRunner.batch(sql,params);


    }

    @Override
    public List<Orders> selectOrdersByUid(int uid) throws Exception {
        //通过uid获取用户的所有订单信息和地址
        String sql = "select o.o_id as oid, o.u_id as uid, o.a_id as aid,o.o_count as ocount," +
                " o.o_time as otime, o.o_state as ostate,a.a_name as aname, " +
                "a.a_phone as aphone , a.a_detail as adetail,a.a_state as astate " +
                "from address a join  orders o on a.a_id = o.a_id where o.u_id = ?";
        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), uid);
        if(Objects.isNull(list)){
            return  null;
        }
        List<Orders> ordersList = new ArrayList<>();
        for(Map<String, Object> map :list){
            Orders orders = new Orders();
            Address address = new Address();
            BeanUtils.populate(orders, map);
            BeanUtils.populate(address, map);
            orders.setAddress(address);
            ordersList.add(orders);
        }
        return ordersList;
    }

    @Override
    public Orders selectOrdersByOid(String oid) throws Exception {
        //通过oid获取 地址和订单信息
        String sql = "select o.o_id as oid, o.u_id as uid, o.a_id as aid,o.o_count as ocount," +
                " o.o_time as otime, o.o_state as ostate,a.a_name as aname, " +
                "a.a_phone as aphone , a.a_detail as adetail,a.a_state as astate " +
                "from address a join  orders o on a.a_id = o.a_id where o.o_id = ?";
        Map<String, Object> map = queryRunner.query(sql, new MapHandler(), oid);
        if(Objects.isNull(map)){
            return  null;
        }
        Orders orders = new Orders();
        Address address = new Address();
        BeanUtils.populate(orders, map);
        BeanUtils.populate(address, map);
        orders.setAddress(address);
        return orders;
    }

    @Override
    public List<Item> selectItemsByOid(String oid) throws Exception {
        //通过oid获取订单中订单项的详细信息(订单项中商品)
        String sql = "select p.p_id as pid,p.t_id as tid,p.p_name as pname,p.p_time as ptime," +
                "p.p_image as pimage,p.p_state as pstate,p.p_info as pinfo ," +
                "p.p_price as pprice,i.o_id as oid,i.i_id as iid,i.i_count as icount," +
                "i.i_num as inum from product p join  item i on p.p_id = i.p_id where i.o_id = ?";
        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), oid);
        if(Objects.isNull(list)){
            return  null;
        }
        List<Item> items = new ArrayList<>();
        for(Map<String, Object> map : list){
            Item item = new Item();
            Product product = new Product();
            //Item这个类中是存在Product属性的
            BeanUtils.populate(product, map);
            BeanUtils.populate(item, map);
            item.setProduct(product);
            items.add(item);

        }

        return items;
    }

    @Override
    public void updateStateByOid(String oid, String state) throws Exception {
        String sql = "update orders set o_state = ? where o_id = ?";
                queryRunner.update(sql,state,oid);
    }
}
